package com.lnsf.bookstore.web.borrow.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.web.book.dao.ApiBookMapper;
import com.lnsf.bookstore.web.book.vo.ApiBook;
import com.lnsf.bookstore.web.borrow.dao.ApiBorrowMapper;
import com.lnsf.bookstore.web.borrow.service.ApiBorrowService;
import com.lnsf.bookstore.web.borrow.vo.ApiBorrow;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ApiBorrowServiceImpl implements ApiBorrowService {

    @Autowired
    private ApiBorrowMapper apiBorrowMapper;

    @Autowired
    private SolrServer solrServer;

    @Autowired
    private ApiBookMapper apiBookMapper;


    @Override
    public int insert(ApiBorrow apiBorrow) {
        return apiBorrowMapper.insertSelective(apiBorrow);
    }

    @Override
    public List<ApiBorrow> queryListByUid(Map<String, Object> map) {
        return apiBorrowMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return apiBorrowMapper.queryTotal(map);
    }

    @Override
    public int delete(Map<String, Object> map) {
        return apiBorrowMapper.delete(map);
    }

    @Override
    public int updateStatus(ApiBorrow apiBorrow) {
        return apiBorrowMapper.updateByPrimaryKeySelective(apiBorrow);
    }

    @Override
    public void adminAllowBorrow(Integer id) {
        //更新借阅状态与起始时间
        ApiBorrow apiBorrow = apiBorrowMapper.queryObject(id);
        apiBorrow.setStatus(0);//借阅中
        apiBorrow.setStartTime(new Date());
        apiBorrowMapper.updateByPrimaryKeySelective(apiBorrow);
        //将对应图书的剩余数量减一
        ApiBook apiBook = apiBookMapper.selectByPrimaryKey(apiBorrow.getBookId());
        apiBook.setBroNum(apiBook.getBroNum() - 1);
        apiBookMapper.updateByPrimaryKeySelective(apiBook);
    }

    @Override
    public void adminAllowReturn(Integer id,Integer delayDay) {
        //更新借阅状态和还书时间
        ApiBorrow apiBorrow = apiBorrowMapper.queryObject(id);
        apiBorrow.setStatus(2);
        apiBorrow.setDelayDay(delayDay);
        apiBorrow.setEndTime(new Date());
        apiBorrowMapper.updateByPrimaryKeySelective(apiBorrow);
        //将对应图书的剩余数量加一
        ApiBook apiBook = apiBookMapper.selectByPrimaryKey(apiBorrow.getBookId());
        apiBook.setBroNum(apiBook.getBroNum() + 1);
        apiBookMapper.updateByPrimaryKeySelective(apiBook);
    }

    @Override
    public ApiBorrow queryObject(Integer borrowId) {
        return apiBorrowMapper.queryObject(borrowId);
    }
}
